package simpleftp.client.control;

import java.io.File;
import java.util.concurrent.Callable;

import simpleftp.client.gui.FTPClientWindow;

/**
 * Simple class to download a file from the server
 * Create a connection to the server on the given directory and download asked file
 */
public final class Downloader extends Loader implements Callable<Boolean> {

	private String 	filePath 		= null;
	private File	destDirectory 	= null;
	
	public Downloader(String s, File f, String currentServerDir, FTPClientWindow window, String server, String port,
			String user, String pass) {
		super(currentServerDir, window, server, port, user, pass);
		
		this.filePath 		= s;
		this.destDirectory 	= f;
	}
	
	@Override
	public Boolean call() throws Exception {
		System.out.println("DOWNLOAD starting for " + filePath);
		Boolean result = false;
		client.connect(server, Integer.parseInt(port), user, pass);
		result = client.retr(destDirectory, filePath);
		if (result) {
			wind.console.append("\nSuccessfully transfered file "
							+ filePath);
			System.out.println("Successfully transfered file "
							+ filePath);
		} else {
			wind.console.append("\nFailed transfering file " + filePath);
			System.out.println("Failed transfering file "
							+ filePath);
		}
		client.disconnect();
		return result;
	}
}
